﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto.People;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.People;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.People;
using VM.IBLL.System.Code;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{


    /// <summary>
    /// 数据字典相关控制器
    /// </summary>
    [Route("api/peopleInfo")]
    [ApiController]
    public class PeopleInfoController : ControllerBase
    {
        //private readonly ICodeHelperBLL _ICodeHelperBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;
        private readonly IPeopleInfoBLL _IPeopleInfoBLL;

        public PeopleInfoController(IJWTService iJWTService, IPeopleInfoBLL iPeopleInfoBLL, IHttpContextAccessor httpContextAccessor)
        {

            _IPeopleInfoBLL = iPeopleInfoBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 添加人员信息（学生、家长）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addPeopleInfo")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addPeopleInfo(addPeopleInfoDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _IPeopleInfoBLL.perfectClass<addPeopleInfoDto>(obj,UserId,ProTypeEnum.save);
            return _IPeopleInfoBLL.addPeopleInfo(obj);
        } 
        
        /// <summary>
        /// 获取人员信息列表，   PHONE+PC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getPeopleInfoList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getPeopleInfoListDto>> getPeopleInfoList(int typeId=0, string? name = "", int pageIndex = 1, int pageSize = 100)
        {
            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            int UserId = Convert.ToInt32(AESUntil.Decrypt(AESId));
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<getPeopleInfoListDto>> Res = _IPeopleInfoBLL.getPeopleInfoList(pageIndex, pageSize,  UserId, name, typeId);
            return Res;
        }
        /// <summary>
        /// 根据人员 信息id获取人员信息详情-需要鉴权 PHONE
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getPeopleInfoById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<getPeopleInfoDetailDto> getPeopleInfoById(int id)
        {
            //解析Jwt,获取到UserId
            int userId = _IJWTService.GetUserIdByAuthInfoStr();
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<getPeopleInfoDetailDto> Res = _IPeopleInfoBLL.getPeopleInfoById(userId,id);
            return Res;
        }


        /// <summary>
        ///修改人员信息（学生、家长）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("updatePeopleInfo")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult updatePeopleInfo(updatePeopleInfoDto obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            //解析并优化类的传参
            obj = _IPeopleInfoBLL.perfectClass<updatePeopleInfoDto>(obj, UserId, ProTypeEnum.update);
            return _IPeopleInfoBLL.updatePeopleInfo(obj);
        }


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("deletePeopleInfoById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult deletePeopleInfoById(int id)
        {
            //解析Jwt,获取到UserId
            int userId = _IJWTService.GetUserIdByAuthInfoStr();
            //校验验证码是否可用，如果可用，返回true
            ApiResult Res = _IPeopleInfoBLL.deletePeoPleInfo(id);
            return Res;
        }

    }
}
